package com.zy.chat.util;

import com.alibaba.fastjson.JSONObject;
import com.zy.chat.constants.constants;
import com.zy.chat.entity.GroupMessages;
import com.zy.chat.entity.SingleMessages;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

import java.util.UUID;


/**
 * @author zy
 * @date 2023/5/15 10:50
 */

@Slf4j
@Component
public class RedisMessageListener implements MessageListener {
    private  static final TimeUtils time=new TimeUtils();

    @Autowired
    private RabbitTemplate rabbitTemplate;

    private RedisMessageListenerContainer container;

    @Autowired
    LettuceConnectionFactory lettuceConnectionFactory;
    @Bean
    public RedisMessageListenerContainer container() {
        container = new RedisMessageListenerContainer();
        container.setConnectionFactory(lettuceConnectionFactory);
        container.addMessageListener(this, new PatternTopic("chat:groupmsg"));
        container.addMessageListener(this, new PatternTopic("chat:singlemsg"));
        return container;
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        String channel = new String(message.getChannel());
        String key = new String(message.getBody());
        JSONObject jsonObject  = JSONObject.parseObject(key);
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        correlationData.getFuture().addCallback(

                result -> {
            if(result.isAck()){
                log.info("消息发送成功");
            }else{
                log.info("消息发送失败,ID:{},原因:{}",correlationData.getId(),result.getReason());
            }
        },ex -> log.error("消息发送异常,ID:{},原因:{}",correlationData.getId(),ex.getMessage()));
        if (channel.equals("chat:singlemsg")) {
            SingleMessages singleMessages = new SingleMessages();
            singleMessages.setContent(jsonObject.getString("content").replace("\n",""));
            singleMessages.setType(jsonObject.getString("type"));
            singleMessages.setChattype(jsonObject.getString("chattype"));
            singleMessages.setFromUser(jsonObject.getString("username"));
            singleMessages.setToUser(jsonObject.getString("touser"));
            singleMessages.setSendTime(time.getCurrentTime());
            rabbitTemplate.convertAndSend(constants.EXCHANGE_NAME_SINGLEMSG,constants.INSERT_KEY_SINGLEMSG,singleMessages,correlationData);
        }else if(channel.equals("chat:groupmsg")){
            GroupMessages groupMessages = new GroupMessages();
            groupMessages.setContent(jsonObject.getString("content").replace("\n",""));
            groupMessages.setType(jsonObject.getString("type"));
            groupMessages.setChattype(jsonObject.getString("chattype"));
            groupMessages.setFromUser(jsonObject.getString("username"));
            groupMessages.setGroupName(jsonObject.getString("touser"));
            groupMessages.setSendTime(time.getCurrentTime());
            rabbitTemplate.convertAndSend(constants.EXCHANGE_NAME_GROUPMSG,constants.INSERT_KEY_GROUPMSG,groupMessages.getId(),correlationData);
        }
    }
}
